package tjs.study.notes.dotest.jvm.class_6_1_类文件结构;

import java.lang.reflect.Field;

/**
 * Created by Nicky.Tang on 2021/10/11 7:41 下午
 *
 * @since 02.12.10
 */
public class 值传递 {
    /**
     * Java没有栈上对象，只有堆上对象，Java的引用都是指针，从这个意义上来说，Java始终是传值的
     * 基本数据类型，直接存的值
     * 引用数据类型/数组，存引用，值在堆中
     *
     */
    public static void main(String[] args) throws Exception {
        UserInfo userInfo = new UserInfo();
        change(userInfo);
        System.out.println(userInfo.userName);//nicky

        Integer integer1 = 130;
        int int2 = 133;
        change(integer1, int2);
        System.out.println(integer1);//130
        System.out.println(int2);//133
        changeSpecial(integer1);
        System.out.println(integer1);//131

        String str1 = "nicky";
        StringBuilder str2 = new StringBuilder("nicky");
        change(str1, str2);
        System.out.println(str1);//nicky
        System.out.println(str2);//nicky
        changeSpecial(str1);
        System.out.println(str1);//nickychange

    }

    public static class UserInfo {
        private String userName = "nicky";

        public void setUserName(String userName) {
            this.userName = userName;
        }
    }

    public static void change(UserInfo userInfo) {
        userInfo = new UserInfo();
        userInfo.setUserName("nicky change");
    }

    public static void change(Integer integer1, int int2) {
        integer1 = integer1 + 1;
        int2 = int2 + 1;
    }

    public static void changeSpecial(Integer integer1) throws Exception {
        Field field = integer1.getClass().getDeclaredField("value");
        field.setAccessible(true);
        field.set(integer1, integer1 + 1);
    }

    public static void change(String str1, StringBuilder str2) {
        str1 = str1 + " change";
        str2 = new StringBuilder();
        str2 = str2.append(" change");
    }

    public static void changeSpecial(String str1) throws Exception {
        Field field = str1.getClass().getDeclaredField("value");
        field.setAccessible(true);
        field.set(str1, new char[]{'n', 'i', 'c', 'k', 'y', 'c', 'h', 'a', 'n', 'g', 'e'});
    }
}
